package com.gy.componit;

import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Dsy
 * @create 2022-02-03  4:38
 */
@Data
public class MyPanel extends JPanel {

    private Set<Component> allComp = new HashSet<>();

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        RenderingHints hints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(hints);
        Map<Integer, List<Component>> map = new HashMap<>();
        synchronized (allComp) {
            for(Component component: allComp) {
                map.putIfAbsent(component.getOrder(), new ArrayList<>());
                map.get(component.getOrder()).add(component);
            }
        }
        List<Integer> orderList = map.keySet().stream().sorted().collect(Collectors.toList());
        for(Integer order: orderList) {
            List<Component> components = map.get(order);
            components.forEach(c -> c.paint(g2d));
        }
    }
}
